package com.example.demo.controller.hr;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.controller.BaseController;
import com.example.demo.entity.*;
import com.example.demo.entity.hr.Resume;
import com.example.demo.enums.PwdEnum;
import com.example.demo.enums.RoleEnum;
import com.example.demo.mapper.*;
import com.example.demo.mapper.hr.ResumeMapper;
import com.example.demo.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/resume")
public class ResumeController extends BaseController {

    @Resource
    ResumeMapper resumeMapper;

    @PostMapping
    public Result<?> save(@RequestBody Resume resume) {
        System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"+resume.getUserName());
        resume.setCreatTime(new Date());
        resumeMapper.insert(resume);
        return Result.success();
    }

    @PutMapping
    public Result<?> update(@RequestBody Resume resume) {
        resumeMapper.updateById(resume);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result<?> update(@PathVariable Long id) {
        resumeMapper.deleteById(id);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result<?> getById(@PathVariable Long id) {
        return Result.success(resumeMapper.selectById(id));
    }

    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String userName,
                              @RequestParam(defaultValue = "") String gangWei,
                              @RequestParam(defaultValue = "") String dataTime,
                              @RequestParam(defaultValue = "") String workYear,
                              @RequestParam(defaultValue = "") String workGrop) {
        LambdaQueryWrapper<Resume> wrapper = Wrappers.<Resume>lambdaQuery();
        if (StrUtil.isNotBlank(userName)) {
            System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"+userName);
            wrapper.like(Resume::getUserName, userName);
        };
        if (StrUtil.isNotBlank(gangWei)) {
            System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"+gangWei);
            wrapper.like(Resume::getGangWei, gangWei);
        };
        if (StrUtil.isNotBlank(dataTime)) {
            System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"+dataTime);
            wrapper.like(Resume::getDataTime, dataTime);
        };
        if (StrUtil.isNotBlank(workYear)) {
            System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"+workYear);
            wrapper.like(Resume::getWorkYear, workYear);
        };
        if (StrUtil.isNotBlank(workGrop)) {
            System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"+workGrop);
            wrapper.like(Resume::getWorkYear, workGrop);
        };



        Page<Resume> newsPage = resumeMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(newsPage);
    }

}
